using Core.SimpleTemp.Application.SysApp.SysFactoryApp;
using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories;
using Core.SimpleTemp.Repositories.Internal;
using Core.Toolkit.BaseLibrary;
using Core.Toolkit.Common;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Core.Toolkit.Core.Application;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Application
{
    [AutoDi()]
    public class ZY_TableApproveProcessService : BaseAppService<ZY_TableApproveProcessDto, ZY_TableApproveProcess, ZY_TableApproveProcessRepository>
    {
        readonly ExcelHelp _excelHelp;
        readonly SysHolidayService _SysHolidayService;
        readonly SysFactoryService _sysFactoryService;
        public ZY_TableApproveProcessService(ZY_TableApproveProcessRepository repository, ExcelHelp excelHelp, SysHolidayService SysHolidayService,
            SysFactoryService sysFactoryService

            ) : base(repository)
        {
            _excelHelp = excelHelp;
            _SysHolidayService = SysHolidayService;
            _sysFactoryService = sysFactoryService;
        }



        #region 基础
        private LoadPageOffsetModelAuto<ZY_TableApproveProcess, ZY_TableApproveProcessDto>
          GetLoadPageOffsetMode(PagingModel<ZY_TableApproveProcessDto>
              pagingQueryModel)
        {
            var loadPageOffsetModel = this.GetLoadPageOffsetModelAuto(pagingQueryModel);
            var baseQuery = _repository.QueryBase().AsNoTracking();
            loadPageOffsetModel.QueryWhere = this.CreateQRList()

     .Add("TableValidityDictId", "=") //作业表格有效期表ID
     .Add("AreaID", "=") //区域
     .Add("HolidayType", "=") //日期类型(正常/特殊日期)
     .Add("ApproveType", "=") //审批类型(正常审批/加审)
     .Add("shift", "=") //班次(不分班次/白班/夜班)
     .Add("ApproverLevel", "=") //第几审批人
     .Add("DutyId", "=") //岗位ID
     .Add("IsDutyOffice", "=") //是否值班主任审批
     .Add("CreateFactory", "=");//隶属工厂


            loadPageOffsetModel.BasqQuery = baseQuery;
            loadPageOffsetModel.Order = orderModel => orderModel.CreateTime;
            loadPageOffsetModel.Selector = model => new ZY_TableApproveProcess()
            {
                Id = model.Id,
                AreaID = model.AreaID,//区域
                HolidayType = model.HolidayType,//日期类型(正常/特殊日期)
                ApproveType = model.ApproveType,//审批类型(正常审批/加审)
                Shift = model.Shift,//班次(不分班次/白班/夜班)
                ApproverLevel = model.ApproverLevel,//第几审批人
                DutyId = model.DutyId,//岗位ID
                IsDutyOffice = model.IsDutyOffice,//是否值班主任审批
                CreateFactory = model.CreateFactory,//隶属工厂
            };
            return loadPageOffsetModel;
        }


        public async Task<IPageModel<ZY_TableApproveProcessDto>> GetList(PagingModel<ZY_TableApproveProcessDto>
            pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            return await base.LoadPageOffsetAsync(loadPageOffsetModel);
        }


        public async Task<string>
            Export(PagingModel<ZY_TableApproveProcessDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            var DataList = await this.ExportDataAsync(loadPageOffsetModel);
            var excelData = DataList.Select(model => new
            {
                
                
                model.AreaID,
                AreaID_title = "区域",
                model.HolidayType,
                HolidayType_title = "日期类型(正常/特殊日期)",
                model.ApproveType,
                ApproveType_title = "审批类型(正常审批/加审)",
                model.Shift,
                shift_title = "班次(不分班次/白班/夜班)",
                model.ApproverLevel,
                ApproverLevel_title = "第几审批人",
                model.DutyId,
                DutyId_title = "岗位ID",
                model.IsDutyOffice,
                IsDutyOffice_title = "是否值班主任审批",
                model.CreateFactory,
                CreateFactory_title = "隶属工厂",
            }).ToList();




            return _excelHelp.Create(excelData);
        }
        #endregion


        /// <summary>
        /// 作业许可证审批列表(申请)
        /// </summary>
        /// <param name="pagingQueryModel"></param>
        public async Task<ZY_TableApproveProcess> JobLicenceApproveAsync(Guid areaId, DateTime approveDateTime, Guid factoryId)
        {
            var isHoliday = _SysHolidayService.IsHoliday(approveDateTime);
            //判断是否是假期
            var IsHolidayOrWeekend = isHoliday || approveDateTime.IsWeekend();
            var holidyType = IsHolidayOrWeekend ? HolidayType.Holidy : HolidayType.Workday;

            //判断是否是涉及倒班 
            Shift shift = Shift.NoShift;
            var factory = await _sysFactoryService.GetFactoryAsync(factoryId);
            if (factory.IsShift)
                if (_sysFactoryService.IsDayShift(factory, approveDateTime))
                    shift = Shift.DayShift;
                else
                    shift = Shift.NightShift;

            var queryBase = _repository.QueryBase();
            queryBase = queryBase.Where(a => a.CreateFactory == factoryId);//区域
            queryBase = queryBase.Where(a => a.AreaID == areaId);//区域
            queryBase = queryBase.Where(a => a.TicketType == TicketTypeEnum.Workermit);//票据类型
            queryBase = queryBase.Where(a => a.HolidayType == holidyType);//是否是节日
            queryBase = queryBase.Where(a => a.ApproveType == ApproveType.NormalApproval);//审批类型
            queryBase = queryBase.Where(a => a.Shift == shift);//班次(不分班次/白班/夜班)
            queryBase = queryBase.Where(a => a.ApproverLevel == 1);//第几审批人
            var entity = await queryBase.FirstOrDefaultAsync();
            return entity;
        }
        

    }
}
